package com.yaoweigroup.community.ui.home;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Constant;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.UserInfo;
import com.yaoweigroup.community.ui.MainTabActivity;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.JsonUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.LoginUtil;
import com.yaoweigroup.community.util.SharePreferenceUtil;
import com.yaoweigroup.community.util.UtilHttp;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * 文体预订
 * 
 * @author xi
 * 
 */
public class SportBookActivity extends Activity implements OnClickListener {

	private RelativeLayout left_rl;
	private EditText yuding_num_et;
	private EditText num_et;
	private EditText hd_name_et;
	private EditText hd_content_et;
	private Button ok_btn;
	private Dialog dialog;
	private SharePreferenceUtil spfUtil;

	public final int SUBMIT_SUCCESS = 0x2210;
	public final int SUBMIT_FAIL = SUBMIT_SUCCESS + 1;
	public final int NEED_LOGIN = SUBMIT_SUCCESS + 2;

	private ObjectMapper mapper;
	private GridView tagView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sport_book);
		mapper = new ObjectMapper();
		spfUtil = new SharePreferenceUtil(this, Constant.prf);
		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);

		yuding_num_et = (EditText) findViewById(R.id.yuding_num_et);
		num_et = (EditText) findViewById(R.id.num_et);
		hd_name_et = (EditText) findViewById(R.id.hd_name_et);
		hd_content_et = (EditText) findViewById(R.id.hd_content_et);
		ok_btn = (Button) findViewById(R.id.ok_btn);
		ok_btn.setOnClickListener(this);

		guid = getIntent().getStringExtra("guid");
		dialog = DialogUtil.createWaitDialog(this, "预订中..");
	}

	private String guid;

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_rl:
			finish();
			break;
		case R.id.ok_btn:
			doSubmit();
			break;
		}
	}

	private void doSubmit() {

		UserInfo userInfo = (UserInfo) spfUtil.loadObject("user");
		String yuding_num = yuding_num_et.getText().toString();
		String num = num_et.getText().toString();
		String hd_name = hd_name_et.getText().toString();
		String hd_content = hd_content_et.getText().toString();
		if (TextUtils.isEmpty(yuding_num) || TextUtils.isEmpty(num)
				|| TextUtils.isEmpty(hd_name) || TextUtils.isEmpty(hd_content)) {

			Toast.makeText(this, "提交的内容不能为空", 400).show();
			return;
		}

		if (userInfo == null) {
			Toast.makeText(this, "请先登录,再预订!", 400).show();
		}
		dialog.show();
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("Uid", userInfo.getUid());
		JsonUtil activityUtil = new JsonUtil();
		activityUtil.add("ActivityName", hd_name);
		activityUtil.add("ActivityContent", hd_name);
		activityUtil.add("PersonAmount", num);

		List<JsonUtil> list = new ArrayList<JsonUtil>();
		JsonUtil orderUtil = new JsonUtil();
		orderUtil.add("SelNum", yuding_num);
		orderUtil.add("seatGuid", guid);
		list.add(orderUtil);

		jsonUtil.add("activity", activityUtil);
		jsonUtil.add("order_list", list);
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString(), "UTF-8");
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}
		LogUtil.d("wxf", "info=" + jsonUtil.toString());
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.scheduleSeat, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {

						LogUtil.d("wxf", responseInfo.result);
						try {
							JSONObject obj = new JSONObject(responseInfo.result);
							int state = obj.getInt("state");
							String msg = obj.getString("message");
							if (state == 200) {
								handler.sendEmptyMessage(SUBMIT_SUCCESS);
							} else if (state == 10000) {
								handler.sendEmptyMessage(NEED_LOGIN);
							} else {
								Message message = handler.obtainMessage();
								message.what = SUBMIT_FAIL;
								message.obj = msg;
								handler.sendMessage(message);
							}
						} catch (Exception e) {
							handler.sendEmptyMessage(SUBMIT_FAIL);
						}
					}

					@Override
					public void onFailure(HttpException error, String msg) {

						handler.sendEmptyMessage(SUBMIT_FAIL);
						LogUtil.d("wxf", msg);
					}

				});
	}

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case SUBMIT_SUCCESS:
				dialog.dismiss();
				Toast.makeText(getApplicationContext(), "预订成功", 500).show();
				Intent intent = new Intent(SportBookActivity.this,
						MainTabActivity.class);
				startActivity(intent);
				break;
			case SUBMIT_FAIL:
				dialog.dismiss();
				String errorMsg = "预订失败!";
				if (msg.obj != null) {
					errorMsg = msg.obj.toString();
				}
				Toast.makeText(getApplicationContext(), errorMsg, 500).show();
				break;
			case NEED_LOGIN:
				LoginUtil.isLoginSuccess(getApplicationContext(), handler,
						mapper);
				break;
			case LoginUtil.LOGIN_SUCCESS:
				doSubmit();
				break;
			case LoginUtil.LOGIN_FAIL:
				dialog.dismiss();
				Toast.makeText(getApplicationContext(), "预订失败!", 500).show();
				break;
			}

		};
	};
}
